function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}

$(function(){
    window.scrollTo(0,document.body.scrollHeight)
    $(".private_letter_ok button").click(function(){
        var content=$(".private_letter_ok input").val()
        var user_id=$(this).attr('user_id')
        var merchant_id=$(this).attr('merchant_id')
        if (!content) {
            alert('请输入内容！')
            return
        }
        var params={
            "user_id":user_id,
            "merchant_id":merchant_id,
            "content":content
        }
        $.ajax({
            url:"/web_private_letter/add",
            type: "post",
            contentType: "application/json",
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            data: JSON.stringify(params),
            success: function (resp) {
                if (resp.errno == "0") {
                    // 添加成功
                    alert("添加成功")
                    window.location.reload()
                }else {
                    alert(resp.errmsg)
                }
            }
        })
    })
    
    $(".private_letter_chat_ok button").click(function(){
        var content=$(".private_letter_chat_ok input").val()
        var receive_id=$(this).attr('receive_id')
        if (!content) {
            alert('请输入内容！')
            return
        }
        var params={
            "receive_id":receive_id,
            "content":content
        }
        $.ajax({
            url:"/web_user_private_letter/chat/add",
            type: "post",
            contentType: "application/json",
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            data: JSON.stringify(params),
            success: function (resp) {
                if (resp.errno == "0") {
                    // 添加成功
                    alert("添加成功")
                    window.location.reload()
                }else {
                    alert(resp.errmsg)
                }
            }
        })
    })
})